package cn.wolfcode.shop.cloud.service.impl;

import cn.wolfcode.shop.cloud.BusinessException;
import cn.wolfcode.shop.cloud.RedisService;
import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.domain.Good;
import cn.wolfcode.shop.cloud.domain.SecKillGood;
import cn.wolfcode.shop.cloud.mapper.SecKillGoodMapper;
import cn.wolfcode.shop.cloud.msg.SecKillCodeMsg;
import cn.wolfcode.shop.cloud.service.ISecKillGoodsService;
import cn.wolfcode.shop.cloud.util.SecKillKeyPrefix;
import cn.wolfcode.shop.cloud.vo.SecKillGoodVO;
import cn.wolfcode.shop.cloud.web.feign.GoodsFeignApi;
import cn.wolfcode.shop.key.KeyPrefix;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SecKillGoodsServiceImpl implements ISecKillGoodsService {

    @Autowired
    private SecKillGoodMapper secKillGoodMapper;

    @Autowired
    private GoodsFeignApi goodsFeignApi;


    @Autowired
    private RedisService redisService;

    @Override
    public List<SecKillGoodVO> query() {
        //查出所有的数据商品
        List<SecKillGood> list = secKillGoodMapper.list();
        Set<Long> goodIds = new HashSet<>();
        for (SecKillGood secKillGood : list) {
            goodIds.add(secKillGood.getGoodId());
        }
        return commonList(goodIds, list);
    }



    @Override
    public SecKillGoodVO find(Long seckillId) {
        ArrayList<SecKillGood> list = new ArrayList<>();
        SecKillGood secKillGood = secKillGoodMapper.get(seckillId);
        list.add(secKillGood);
        Set<Long> goodIds = new HashSet<>();
        goodIds.add(secKillGood.getGoodId());
        return commonList(goodIds, list).get(0);
    }

    @Override
    public int decrCount(Long seckillId) {
        return secKillGoodMapper.decrCount(seckillId);
    }




    private List<SecKillGoodVO> commonList(Set<Long> goodIds, List<SecKillGood> list) {
        Result<List<Good>> listResult = goodsFeignApi.queryGoods(goodIds);
        if (listResult == null || listResult.getCode() != 200) {
            throw new BusinessException(SecKillCodeMsg.REMOTE_GOODS_NULL);
        }

        List<Good> goods = listResult.getData();
        HashMap<Long, Good> goodsMap = new HashMap<>();
        for (Good good : goods) {
            goodsMap.put(good.getId(), good);
        }
        List<SecKillGoodVO> listVO = new ArrayList<>();
        for (SecKillGood secKillGood : list) {
            SecKillGoodVO vo = new SecKillGoodVO();
            Good good = goodsMap.get(secKillGood.getGoodId());
            BeanUtils.copyProperties(good, vo);
            BeanUtils.copyProperties(secKillGood, vo);
            listVO.add(vo);
        }
        return listVO;
    }



    @Override
    public List<SecKillGoodVO> queryByCache() {
        Map<String, SecKillGoodVO> resultMap = redisService.hgetAll((KeyPrefix) SecKillKeyPrefix.SECKILL_GOOD, "", SecKillGoodVO.class);
        return new ArrayList<>(resultMap.values());
    }


    @Override
    public SecKillGoodVO findByCache(Long seckillId) {
        SecKillGoodVO sekillvo = redisService.hget(SecKillKeyPrefix.SECKILL_GOOD, "", seckillId + "", SecKillGoodVO.class);
        Integer count = redisService.get(SecKillKeyPrefix.SECKILL_STOCK, "" + seckillId, Integer.class);
        if(count<0){
            count = 0;
        }
        sekillvo.setStockCount(count);
        return sekillvo;
    }

    @Override
    public void syncPreCount(Long goodId) {

        SecKillGoodVO secKillGoodVO = find(goodId);
        redisService.set(SecKillKeyPrefix.SECKILL_STOCK,secKillGoodVO.getId()+"",secKillGoodVO.getStockCount());

    }

}
